﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SharingCenter.Web.Classes.Visits
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using System.Web.UI.WebControls;
    using System.Collections;

    public class VisitOperations
    {
        public static void AddVisit(EntityType entity, Guid entityId, Guid userId)
        {
            try
            {

                if (entityId != userId)
                {
                    scDataContext.Instance.AddVisit((int)entity, entityId, userId);
                }
            }
            catch { }
        }

        public static List<Sc_SharedVisit> GetVisitors(EntityType entity, Guid entityId, SortDirection direction)
        {
            List<Sc_SharedVisit> ret = null;
            if (direction == SortDirection.Ascending)
            {
                var q = from t in scDataContext.Instance.Sc_SharedVisits
                    where t.ContextId == entityId && t.ContextType == entity
                    orderby t.VisitDate ascending
                    select t;
                ret = q.ToList();
            }
            else
            {
                var q = from t in scDataContext.Instance.Sc_SharedVisits
                    where t.ContextId == entityId && t.ContextType == entity
                    orderby t.VisitDate descending
                    select t;
                ret = q.ToList();
            }
            return ret;
        }

        public static List<Sc_SharedVisit> GetVisitors(EntityType entity, Guid entityId, SortDirection direction, int limit)
        {
            List<Sc_SharedVisit> ret = null;
            using (scDataContext cxt = new scDataContext())
            {
                if (direction == SortDirection.Ascending)
                {
                    var q = (from t in cxt.Sc_SharedVisits
                             where t.ContextId == entityId && t.ContextType == entity
                             orderby t.VisitDate ascending
                             select t).Take(limit);
                    ret = q.ToList();
                }
                else
                {
                    var q = (from t in cxt.Sc_SharedVisits
                             where t.ContextId == entityId && t.ContextType == entity
                             orderby t.VisitDate descending
                             select t).Take(limit);
                    ret = q.ToList();
                }
            }
            return ret;
        }
    }
}
